package servlet;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Category;
import model.ExpiredQuest;
import model.ExpiredStudy;
import model.Participation;
import model.Quest;
import model.Study;
import DAO.ExpiredQuestDao;
import DAO.ExpiredStudyDao;
import DAO.ParticipationDao;
import DAO.StudyDao;
import DAO.UserDetailDao;

public class StudyListAction implements Action{
	
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("UTF-8"); 
		ActionForward forward=new ActionForward();
		ArrayList<Category> categories = new ArrayList<Category>();
		ArrayList <Study> studyList = new ArrayList <Study>();
		
		String keyword=null;
		
		StudyDao studyDao = new StudyDao();
		categories = studyDao.getAllCategory();
		

		request.setAttribute("checkValue", "checkValue");
		request.setAttribute("category",categories);
		
		request.setCharacterEncoding("UTF-8"); 
		int category = request.getParameter("category")!=null
				&&request.getParameter("category")!="" ? Integer.parseInt(request.getParameter("category")):0;
				
		keyword = request.getParameter("keyword");
		
		if(category>0 && keyword!=null && keyword!="")
		{
			studyList = studyDao.getDetailSearchStudyList(category, keyword);
		}
		else if(category>0)
		{
			studyList = studyDao.getCategoryStudyList(category);
		}
		else if(keyword!=null && keyword !="")
		{
			System.out.println(keyword);
			studyList = studyDao.getSearchStudyList(keyword);	
		}
		else			
		{
			studyList = studyDao.getAllStudyList();			
		}
		
		
		Date now = new Date();
		long timeTemp = now.getTime() - 86400000;

		Timestamp nowTimeStamp = new Timestamp((timeTemp));
		
		for (int i = 0; i < studyList.size(); i++) {

			Study study = studyDao
					.getStudyInfo(studyList.get(i).getStudyid());
			ExpiredStudyDao expiredStudyDao = new ExpiredStudyDao();

			if (expiredStudyDao.getExpiredStudy(study.getStudyid())) {
			
				studyList.get(i).setExpired(true);
				System.out.println(studyList.get(i).getStudyid()+" "+studyList.get(i).isExpired());
			} else {
				
				Timestamp expiredTimeStamp = study.getExpiredTime();
				
				if (nowTimeStamp.after(expiredTimeStamp)) {
				
					ExpiredStudy expiredStudy = new ExpiredStudy();
					expiredStudy.setSid(study.getStudyid());
					expiredStudy.setExpiredTime(expiredTimeStamp);
					expiredStudyDao.insertExpiredStudy(expiredStudy);
					
					ParticipationDao participationDao = new ParticipationDao();
					ArrayList <Participation> participationList = participationDao.getSameStudyMember(study.getStudyid());
					
					UserDetailDao userDetailDao = new UserDetailDao();
					
					
					for(int j=0; j< participationList.size(); j++)
					{
	
					 userDetailDao.updateClearStudy(participationList.get(j).getUid());
					}
					
					studyList.get(i).setExpired(true);
					
				}
			}

		}

		
	
		
		
		request.setAttribute("studyList", studyList);
		
		forward.setPath("./questudy.jsp");
		
		return forward;
	}
}
